package com.anzia.set;

import java.util.Comparator;
import java.util.TreeSet;

/**
 *@ClassName TreeSet1_Demo
 *@Description
 *@Author Anzia.
 *@Date 2021/4/10 15:00
 *@Version 1.0
 **/
public class TreeSet1_Demo {
    public static void main(String[] args) {
        //创建集合，并指定比较规则
        TreeSet<Person> treeSet = new TreeSet<>(new Comparator<Person>() {
            @Override
            public int compare(Person o1, Person o2) {
                int n1 = o1.getAge() - o2.getAge();
                int n2 = o1.getName().compareTo(o2.getName());
                return n1 == 0 ? n2 : n1;
            }
        });

        Person person1 = new Person("刘德华", 20);
        Person person2 = new Person("周星驰", 21);
        Person person3 = new Person("郭德纲", 22);

        treeSet.add(person1);
        treeSet.add(person2);
        treeSet.add(person3);
        System.out.println(treeSet);
    }
}
